package dk.dashboardFP.panels.displayPanel {
	import dk.dashboardFP.dfpLogger.DFPLogger;
	import dk.dashboardFP.utils.keyListener.KeyListener;

	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.utils.Dictionary;

	/**
	 * @author josef
	 */
	public class GlobalViewControl {

		private var _oDisplayPanel : DisplayPanel;
		private var _oDisplayeditorControl : DisplayEditorControl;
		private var _bInMoveMode : Boolean;
		private var _pCurrentDrag : Point;
		private var _doStage : Stage;
		private var _a_doElements : Array;
		private var _oDict : Dictionary;
		private var _currentZoom : Number = 1;
		private var _zoomStep : Number = 0.1;

		public function GlobalViewControl(oDisplayPanel : DisplayPanel) {
			_oDisplayPanel = oDisplayPanel;
			
			setupElements();
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// setup
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function setupElements() : void {
			setupElementsHandler();
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// stage object handler
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function setupElementsHandler() : void {
			
			//:::::::
			// containers 
			_a_doElements = [];
			
			//:::::::
			// register 
			_doStage = _oDisplayPanel.doDashboard.doStage;
			_doStage.addEventListener(Event.ADDED, onAdded, false, 0, true);
			_doStage.addEventListener(Event.REMOVED, onRemoved);
			
			// register for key events 
		//	KeyListener.getInstance().addKeyDown(107, onZoomInKey);
		//	KeyListener.getInstance().addKeyDown(109, onZoomOutKey);
			
			//:::::::
			// register current 
			for (var i : Number = 0;i < _doStage.numChildren;i++) {
				if (_doStage.getChildAt(i) != _oDisplayPanel.doDashboard) {
					registerElement(_doStage.getChildAt(i));
				}
			}
		}

		private function onRemoved(event : Event) : void {
			unregisterElement(event.target as DisplayObject);
		}

		private function onAdded(event : Event) : void {
			if (DisplayObject(event.target).parent == _doStage) {
				registerElement(event.target as DisplayObject);
			}
		}		

		private function registerElement(doAddedDO : DisplayObject) : void {
			_a_doElements.push(doAddedDO);
			applyCurrentTransform(doAddedDO);
		}

		private function unregisterElement(doRemovedDO : DisplayObject) : void {
			if(_a_doElements.indexOf(doRemovedDO) != -1) {
				_a_doElements.splice(_a_doElements.indexOf(doRemovedDO), 1);
			}
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// zoom action handler
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		/*
		private function onZoomInKey() : void {
			_currentZoom += _zoomStep;
			for each (var child : DisplayObject in _a_doElements) {
				child.scaleX = child.scaleY = _currentZoom;
				child.x = child.x * _currentZoom;
				child.y = child.y * _currentZoom;
			}
		}

		private function onZoomOutKey() : void {
			DFPLogger.log("Zoom out");
		}
		 * 
		 */

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// transform
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function applyCurrentTransform(doAddedDO : DisplayObject) : void {
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// register controller
		//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 

		public function registerController(oDisplayeditorControl : DisplayEditorControl) : void {
			_oDisplayeditorControl = oDisplayeditorControl;
			_oDisplayeditorControl.addEventListener(DisplayEditorControlEvent.CHANGED_MODE, onControlModeChange, false, 0, true);
		}

		private function onControlModeChange(event : DisplayEditorControlEvent) : void {
			if (_oDisplayeditorControl.sCurrentDisplayEditorMode == DisplayEditorModes.PAN) {
				enterMoveMode();
			} else if (_bInMoveMode) {
				exitMoveMode();
			}
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// move mode
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function enterMoveMode() : void {
			_bInMoveMode = true;
			
			_oDisplayPanel.oDragController.addEventListener(DragControllerEvent.START_DRAG, onStartDrag);
			_oDisplayPanel.oDragController.addEventListener(DragControllerEvent.END_DRAG, onEndDrag);
		}

		private function exitMoveMode() : void {
			_bInMoveMode = false;

			_oDisplayPanel.oDragController.removeEventListener(DragControllerEvent.START_DRAG, onStartDrag);
			_oDisplayPanel.oDragController.removeEventListener(DragControllerEvent.END_DRAG, onEndDrag);
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// drag handlers
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function onEndDrag(event : DragControllerEvent) : void {
			_oDisplayPanel.oDragController.removeEventListener(DragControllerEvent.UPDATE_DRAG, onDragUpdate);
		}

		private function onStartDrag(event : DragControllerEvent) : void {
			_oDisplayPanel.oDragController.addEventListener(DragControllerEvent.UPDATE_DRAG, onDragUpdate);
			_pCurrentDrag = _oDisplayPanel.oDragController.pCurrentDrag;
			
			_oDict = new Dictionary();
			for each (var doElements : DisplayObject in _a_doElements) {
				_oDict[doElements] = new Point(doElements.x, doElements.y);
			}
		}

		private function onDragUpdate(event : DragControllerEvent) : void {
			for each (var doElements : DisplayObject in _a_doElements) {
				
				var pBasePos : Point = _oDict[doElements] as Point;
				
				doElements.x = pBasePos.x + _pCurrentDrag.x;
				doElements.y = pBasePos.y + _pCurrentDrag.y;
			}
		}
	}
}
